package com.hillky.discovery.impl;

import com.hillky.Constant;
import com.hillky.ServiceConfig;
import com.hillky.YrpcBootstrap;
import com.hillky.discovery.AbstractRegistry;
import com.hillky.utils.NetUtils;
import com.hillky.utils.zookeeper.ZookeeperNode;
import com.hillky.utils.zookeeper.ZookeeperUtils;
import com.hillky.watcher.UpAndDownWatcher;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.stream.Collectors;

public class ZookeeperRegistry extends AbstractRegistry {

    private  ZooKeeper zookeeper;

    private static int port=8094;

    public ZookeeperRegistry() {
        zookeeper=ZookeeperUtils.createZookeeper();
    }

    public ZookeeperRegistry(ZooKeeper zookeeper) {
        this.zookeeper = zookeeper;
    }

    public ZookeeperRegistry(String connectString,int timeout) {
        this.zookeeper = ZookeeperUtils.createZookeeper(connectString,timeout);
    }


    @Override
    public void register(ServiceConfig<?> service) {
        //服务名称的节点
        String parentNode = Constant.BASE_PROVIDERS_PATH + "/" + service.getInterface().getName();

        // 创建父亲持久节点
        if(!ZookeeperUtils.exists(zookeeper,parentNode,null)){
            ZookeeperNode zookeeperNode = new ZookeeperNode(parentNode, null);
            ZookeeperUtils.createNode(zookeeper,zookeeperNode,null, CreateMode.PERSISTENT);
        }

        // 建立分组节点
        parentNode=parentNode+"/"+service.getGroup();
        if(!ZookeeperUtils.exists(zookeeper,parentNode,null)){
            ZookeeperNode zookeeperNode = new ZookeeperNode(parentNode, null);
            ZookeeperUtils.createNode(zookeeper,zookeeperNode,null, CreateMode.PERSISTENT);
        }

        //创建临时节点
        //节点名称: ip+端口
        String node = parentNode + "/" + NetUtils.getIp() + ":"+ YrpcBootstrap.getInstance().getConfiguration().getPort();
        //创建子节点
        if(!ZookeeperUtils.exists(zookeeper,node,null)){
            ZookeeperNode zookeeperNode = new ZookeeperNode(node, null);
            ZookeeperUtils.createNode(zookeeper,zookeeperNode,null, CreateMode.EPHEMERAL);
        }

    }

    /**
     * 注册中心的核心目的是什么？拉取合适的服务列表
     * @param name 服务名称
     * @return 服务列表
     */
    @Override
    public List<InetSocketAddress> lookup(String name,String group) {
        //1.找到对应服务节点
        String serviceNode = Constant.BASE_PROVIDERS_PATH + "/" + name+"/"+group;

        //2.总zk中获取他的子节点
        List<String> children = ZookeeperUtils.getChildren(zookeeper, serviceNode, new UpAndDownWatcher());

        List<InetSocketAddress> list = children.stream().map(str -> {
            String[] ipAndPort = str.split(":");
            InetSocketAddress inetSocketAddress = new InetSocketAddress(ipAndPort[0], Integer.valueOf(ipAndPort[1]));
            return inetSocketAddress;
        }).collect(Collectors.toList());

        return list;

    }
}
